package day07.test1;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class test1 {
    //猜字母游戏
    public static void main(String[] args) {
        System.out.println("猜字母游戏，请输入5个字母：");
        //用户输入
        String[] my_arr =new String[5];
        //电脑输入，随机生成
        String[] pc_arr =test1.stochastic();
        System.out.println(Arrays.toString(pc_arr));

        while(true)
        {

            //接收字符串并判断
            int count = test1.receiveStr(my_arr);
            if(count == 0)
            {
                System.out.println("程序结束，已退出");
                break;
            }

            //比较
            int judge = test1.strcmp(my_arr,pc_arr);
            if(judge == 1)
            {
                System.out.println("恭喜你，游戏胜利！");
                break;
            }

        }



    }
    //接收字符串
    public static int receiveStr(String[] my_arr)
    {
        Scanner scan = new Scanner(System.in);
        for(int i = 0;i<my_arr.length;i++)
        {
            my_arr[i] = scan.next();
            //判断输入为0
            if(my_arr[i].equals("0"))
                return 0;
        }
        System.out.println(Arrays.toString(my_arr));
        return 1;
    }

    //生成随机字母
    public static String[] stochastic()
    {
        //随机生成的26字母范围
        String[] codeStr = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "y", "u", "v", "w", "x", "y", "z"};
        boolean[] letter = new boolean[codeStr.length];
        Random ran = new Random();
        String[] arr= new String[5];
        //去重
        for(int i = 0;i<arr.length;i++)
        {
            int index;
            do{
                index = ran.nextInt(26);
            }while(letter[index] == true);
            arr[i] = codeStr[index];
            letter[index] = true;
        }
        return arr;
    }


    //比较字符串
    public static int strcmp(String[] my_arr,String[] pc_arr)
    {
        if(my_arr.length <=0)
        {
            return -1;
        }
        //比较数据
        //全部正确 返回1
        // 否则返回0

        int strEqual = 0,positionEqual = 0,fraction = 999;
        for(int i = 0;i<pc_arr.length;i++)
        {
            for(int j = 0;j<my_arr.length;j++)
            {
                if(pc_arr[i].equals(my_arr[j]))
                {
                    strEqual++;
                    if(i == j)
                    {
                        positionEqual++;
                    }
                    break;
                }
                else
                {
                    fraction -= 1;
                }
            }
        }
        System.out.print("strEqual(字母正确个数):"+strEqual+"  ");
        System.out.print("positionEqual(位置正确个数):"+positionEqual+"  ");
        System.out.println("fraction(得分):"+fraction+"  ");
        if(strEqual == positionEqual && strEqual == 5)
        {
            return 1;
        }
        else if(strEqual == 5 && positionEqual !=5)
        {
            System.out.println("字母全部正确");
            System.out.println("位置错误"+(5-positionEqual)+"个，请重新输入：");
        }
        else if(positionEqual ==5)
        {
            System.out.println("全部正确");
        }
        else
        {
            System.out.println("位置错误"+(5-positionEqual)+"个");
            System.out.println("字母错误"+(5-strEqual)+"个");
            System.out.println("请重新输入");

        }
        return 0;
    }
}
